package com.info.zhiduoduo.core.service.common;

import com.baomidou.mybatisplus.mapper.Condition;
import com.info.zhiduoduo.common.utils.DateUtil;
import com.info.zhiduoduo.repository.entity.project.ProjectFileRecord;
import com.info.zhiduoduo.repository.mapper.project.ProjectFileRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author tianyonghong
 * @create 2022/7/21
 */
@Slf4j
@Service
public class ProjectFileService {

    @Resource
    private ProjectFileRecordMapper projectFileRecordMapper;

    /**
     * 新建文件记录
     */
    public Integer insertFileName(int projectType, Integer compId, String filename,String filePath) {
        ProjectFileRecord record = new ProjectFileRecord();
        record.setCompid(compId);
        record.setProjectType(projectType);
        record.setSourceFileName(filename);
        record.setFilePath(filePath);
        record.insert();
        return record.getId();
    }

    public void delById(Integer id){
        projectFileRecordMapper.deleteById(id);
    }

    /**
     * 查询文件名称是否存在
     * @return 存在返回true
     */
    public boolean checkFileExist(int projectType, Integer compId, String filename) {
        List<ProjectFileRecord> recordList = projectFileRecordMapper.selectList(Condition.create()
                                                                                         .eq("compid", compId)
                                                                                         .eq("project_type", projectType)
                                                                                         .eq("source_file_name", filename));
        if (CollectionUtils.isNotEmpty(recordList)) {
            return true;
        }
        return false;
    }
    /**
     * 校验当日查询文件名称是否存在
     * @return 存在返回true
     */
    public boolean onTheDayCheckFileExist(int projectType, Integer compId, String filename) {
        List<ProjectFileRecord> recordList = projectFileRecordMapper.onTheDayCheckFileExist(projectType,compId,filename,DateUtil.getNowDateString(DateUtil.FORMAT_STYLE_2));
        if (CollectionUtils.isNotEmpty(recordList)) {
            return true;
        }
        return false;
    }

}
